<!--
* description: 搜索反馈
* author: zouzx
* createdDate: 2019-12-24
-->
<template>
  <section class="crmDlrJoinMemberSet">
    <el-dialog v-dialogDrag
      :close-on-click-modal="false"
      :title="textMap[popupsState]"
      :append-to-body="true"
      :visible.sync="curPopupsVisible"
      @close="sendCode"
      width="1000px"
    >
      <dialogHeader
        slot="title"
        :title="textMap[popupsState]"
        :styleType="$store.state.app.dialogType"
      ></dialogHeader>
      <div class="filter-container filter-params">
        <el-row :gutter="26" ref="vailComponentList">
          <component
            v-for="comp in tableComponents.filter(o => o.isMust === true)"
            :ref="comp.isRequire ? comp.isRequire+ comp.compKey : comp.compKey"
            :key="comp.compKey"
            :codeField="comp.codeField"
            :textField="comp.textField"
            :popupsKey="comp.compKey"
            :is="comp.component"
            :code="formField[comp.codeField]"
            :text="formField[comp.textField]?formField[comp.textField]:''"
            @changeCode="getComponentCode"
            @focusCode="getFocusCode"
            @resultTypeChange="changeauditType"
            :disabled="comp.disabled"
            :isRequire="comp.isRequire"
            :isMul="comp.isMul"
            :span="comp.span || 8"
            :inputType="comp.type"
            :labelName="comp.labelName"
            :lookuptype="comp.lookuptype"
            :dateOptionsType="comp.dateOptionsType"
          ></component>
        </el-row>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button
          v-for="comp in tableButtons"
          :key="comp.compKey"
          :type="comp.type"
          @click="comp.clickEvent"
        >{{comp.text}}</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
import { formMixins } from "@/components/mixins/formMixins";
import { crmApis } from "@/api/apiList/crm";
import { requestAPI } from "@/api/commonRequest";
export default {
  // 组件混入对象
  mixins: [formMixins],  components: {
    dialogHeader: () => import("@/components/dialogHeader") //弹窗标题
  },
  data() {
    return {
      //多选对象
      checkList: [],
      // 保存API配置对象
      apiConfig: crmApis.kmFeedbackMutationSaveById,
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: "btnKey1",
          type: "primary",
          size: "small",
          clickEvent: () => this.save(),
          text: "保存"
        }
      ],
      // 动态组件-查询条件
      tableComponents: [
        {
          compKey: "compKey1",
          labelName: "搜索词",
          codeField: "feedbackTitle",
          isRequire: false,
          component: () => import("@/components/org/commonInput"),
          disabled: true,
          type: "inputText",
          isMust: true,
          span: 16
        },
        {
          compKey: "compKey2",
          labelName: "结果反馈",
          codeField: "feedbackType",
          isRequire: true,
          lookuptype: "KN0003",
          component: () => import("@/components/org/LookupValue"),
          type: "dropdownList",
          isMust: true,
          isMul: false,
          isRequire: true,
        },
        {
          compKey: "compKey3",
          labelName: "反馈内容",
          codeField: "feedbackContent",
          isRequire: true,
          component: () => import("@/components/crm/textbox/crmTextInput"),
          type: "textarea",
          isMust: true,
          disabled: false,
          span: 16
        }
      ],
      // 标题
      textMap: {
        edit: "搜索反馈",
        add: "搜索反馈"
      },
      // 表单数据（无需赋值，由混入对象赋值）
      formField: {
        feedbackTitle: "",
        feedbackType: "",
        feedbackContent: ""
      }
    };
  },
  beforeUpdate() {
    this.$nextTick(function() {
      this.changeauditType(this.formField.feedbackType);
    });
  },
  created() {
    if (this.popupsState === "edit") {
      this.formField.feedbackTitle=this.dynamicEditRowData.feedbackTitle
      this.formField.feedbackType="1"
    }
  },
  methods: {
    changeauditType(val) {
      let that = this;
      if(val)
      {
        switch (val) {
          case "0": {
            this.$refs.truecompKey3[0].disabled = true;
            this.$refs.truecompKey3[0].isRequire = false;
            this.formField.feedbackContent="";
            break;
          }
          case "1": {
            this.$refs.truecompKey3[0].disabled = false;
            this.$refs.truecompKey3[0].isRequire = true;
            break;
          }
        }
      }
    },
    save() {
      let that = this;
      debugger;
      var html = "";
      //必填检验
      if (!that.formField.feedbackTitle) {
        html = html + `<div class="sys-alertDiv">搜索词不能为空</div>`;
      }
      if (!that.formField.feedbackType) {
        html = html + `<div class="sys-alertDiv">请选择结果反馈</div>`;
      }
      if (that.formField.feedbackType == "1"&&!that.formField.feedbackContent) {
        html = html + `<div class="sys-alertDiv">请填写反馈内容</div>`;
      }
      if(that.formField.feedbackContent){
        var temp = 0;
        for (var i = 0; i < that.formField.feedbackContent.length; i++) {
          if (/[\u4e00-\u9fa5]/.test(that.formField.feedbackContent[i])) {
            temp += 2;
          } else {
            temp++;
          }
        }
        if (temp > 200) {
          html = html + `<div class="sys-alertDiv">反馈内容不能超过200个字符</div>`;
        }
      }
      if (html != "") {
        this.$crmcf.showMessages(this, 'error', html, true)
        return;
      }
      this.saveForm();
      this.curPopupsVisible=false;
    }
  }
};
</script>
